<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<!-- qscriptclass.cpp -->
<head>
  <title>QScriptClass Class Reference</title>
  <link href="classic.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="57" height="67" border="0" /></td>
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a></td>
</tr></table><h1 class="title">QScriptClass Class Reference<br /><span class="small-subtitle">[<a href="http://qt.nokia.com/doc/4.6/qtscript.html">QtScript</a> module]</span>
</h1>
<p>The QScriptClass class provides an interface for defining custom behavior of (a class of) Qt Script objects. <a href="#details">More...</a></p>
<pre> #include &lt;QScriptClass&gt;</pre><p><b>This class is not part of the Qt GUI Framework Edition.</b></p>
<p>This class was introduced in qtscriptclassic 4.4.</p>
<ul>
<li><a href="qscriptclass-members.html">List of all members, including inherited members</a></li>
</ul>
<hr />
<a name="public-types"></a>
<h2>Public Types</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top">enum </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#Extension-enum">Extension</a></b> { Callable, HasInstance }</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">enum </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#QueryFlag-enum">QueryFlag</a></b> { HandlesReadAccess, HandlesWriteAccess }</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">flags </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#QueryFlag-enum">QueryFlags</a></b></td></tr>
</table>
<hr />
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#QScriptClass">QScriptClass</a></b> ( QScriptEngine * <i>engine</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#dtor.QScriptClass">~QScriptClass</a></b> ()</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">QScriptEngine * </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#engine">engine</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QVariant </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#extension">extension</a></b> ( Extension <i>extension</i>, const QVariant &amp; <i>argument</i> = QVariant() )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QString </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#name">name</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QScriptClassPropertyIterator * </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#newIterator">newIterator</a></b> ( const QScriptValue &amp; <i>object</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QScriptValue </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#property">property</a></b> ( const QScriptValue &amp; <i>object</i>, const QScriptString &amp; <i>name</i>, uint <i>id</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QScriptValue::PropertyFlags </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#propertyFlags">propertyFlags</a></b> ( const QScriptValue &amp; <i>object</i>, const QScriptString &amp; <i>name</i>, uint <i>id</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QScriptValue </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#prototype">prototype</a></b> () const</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual QueryFlags </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#queryProperty">queryProperty</a></b> ( const QScriptValue &amp; <i>object</i>, const QScriptString &amp; <i>name</i>, QueryFlags <i>flags</i>, uint * <i>id</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual void </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#setProperty">setProperty</a></b> ( QScriptValue &amp; <i>object</i>, const QScriptString &amp; <i>name</i>, uint <i>id</i>, const QScriptValue &amp; <i>value</i> )</td></tr>
<tr><td class="memItemLeft" align="right" valign="top">virtual bool </td><td class="memItemRight" valign="bottom"><b><a href="qscriptclass.html#supportsExtension">supportsExtension</a></b> ( Extension <i>extension</i> ) const</td></tr>
</table>
<a name="details"></a>
<hr />
<h2>Detailed Description</h2>
<p>The QScriptClass class provides an interface for defining custom behavior of (a class of) Qt Script objects.</p>
<p>The QScriptClass class defines an interface for handling various aspects of interaction with the Qt Script objects associated with the class. Such objects are created by calling <a href="qscriptengine.html#newObject">QScriptEngine::newObject</a>(), passing a pointer to the QScriptClass as argument.</p>
<p>By subclassing QScriptClass, you can define precisely how access to properties of the objects that use your class is handled. This enables a fully dynamic handling of properties, e.g&#x2e; it's more powerful than <a href="qscriptengine.html#newQObject">QScriptEngine::newQObject</a>(). For example, you can use QScriptClass to implement array-type objects (i.e&#x2e; objects that handle the <tt>length</tt> property, and properties whose names are valid array indexes, in a special way), or to implement a &quot;live&quot; (runtime-defined) proxy to an underlying object.</p>
<p>If you just need to handle access to a set of properties that are known at the time an object is created (i.e&#x2e; &quot;semi-statically&quot;), you might consider using <a href="qscriptvalue.html#setProperty">QScriptValue::setProperty</a>() to define getter/setter functions for the relevant properties, rather than subclassing QScriptClass.</p>
<p>Reimplement <a href="qscriptclass.html#queryProperty">queryProperty</a>() to specify which properties are handled in a custom way by your script class (i.e&#x2e; should be <b>delegated</b> to the QScriptClass), and which properties should be handled just like normal Qt Script object properties.</p>
<p>Reimplement <a href="qscriptclass.html#property">property</a>() and <a href="qscriptclass.html#setProperty">setProperty</a>() to perform the actual access (read or write) to the properties that your class handles. Additionally, you can reimplement <a href="qscriptclass.html#propertyFlags">propertyFlags</a>() to specify custom flags for your properties.</p>
<p>Reimplement <a href="qscriptclass.html#newIterator">newIterator</a>() to provide an iterator for objects of your custom class. This is only necessary if objects of your class can have custom properties that you want to be reported when an object is used together with the <a href="qscriptvalueiterator.html">QScriptValueIterator</a> class, or when an object is used in a for-in enumeration statement in a script.</p>
<p>When implementing custom classes of objects, you typically use <a href="qscriptvalue.html#setData">QScriptValue::setData</a>() to store instance-specific data as part of object initialization; the data won't be accessible from scripts directly, but you can access it in e.g&#x2e; your reimplementations of <a href="qscriptclass.html#property">property</a>() and <a href="qscriptclass.html#setProperty">setProperty</a>() (by calling <a href="qscriptvalue.html#data">QScriptValue::data</a>()) to perform custom processing.</p>
<p>Reimplement <a href="qscriptclass.html#prototype">prototype</a>() to provide a custom prototype object for your script class.</p>
<p>Reimplement <a href="qscriptclass.html#supportsExtension">supportsExtension</a>() and <a href="qscriptclass.html#extension">extension</a>() if your custom script class supports one or more of the extensions specified by the Extension enum.</p>
<p>See also <a href="qscriptclasspropertyiterator.html">QScriptClassPropertyIterator</a>, <a href="qscriptengine.html#newObject">QScriptEngine::newObject</a>(), and <a href="http://qt.nokia.com/doc/4.6/script-customclass.html">Custom Script Class Example</a>.</p>
<hr />
<h2>Member Type Documentation</h2>
<h3 class="fn"><a name="Extension-enum"></a>enum QScriptClass::Extension</h3>
<p>This enum specifies the possible extensions to a <a href="qscriptclass.html">QScriptClass</a>.</p>
<p><table class="valuelist" border="1" cellpadding="2" cellspacing="1" width="100%">
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
<tr><td valign="top"><tt>QScriptClass::Callable</tt></td><td align="center" valign="top"><tt>0</tt></td><td valign="top">Instances of this class can be called as functions.</td></tr>
<tr><td valign="top"><tt>QScriptClass::HasInstance</tt></td><td align="center" valign="top"><tt>1</tt></td><td valign="top">Instances of this class implement [[HasInstance]].</td></tr>
</table></p>
<p>See also <a href="qscriptclass.html#extension">extension</a>().</p>
<h3 class="flags"><a name="QueryFlag-enum"></a>enum QScriptClass::QueryFlag<br />flags QScriptClass::QueryFlags</h3>
<p>This enum describes flags that are used to query a <a href="qscriptclass.html">QScriptClass</a> regarding how access to a property should be handled.</p>
<p><table class="valuelist" border="1" cellpadding="2" cellspacing="1" width="100%">
<tr><th width="25%">Constant</th><th width="15%">Value</th><th width="60%">Description</th></tr>
<tr><td valign="top"><tt>QScriptClass::HandlesReadAccess</tt></td><td align="center" valign="top"><tt>0x01</tt></td><td valign="top">The <a href="qscriptclass.html">QScriptClass</a> handles read access to this property.</td></tr>
<tr><td valign="top"><tt>QScriptClass::HandlesWriteAccess</tt></td><td align="center" valign="top"><tt>0x02</tt></td><td valign="top">The <a href="qscriptclass.html">QScriptClass</a> handles write access to this property.</td></tr>
</table></p>
<p>The QueryFlags type is a typedef for <a href="qflags.html">QFlags</a>&lt;QueryFlag&gt;. It stores an OR combination of QueryFlag values.</p>
<p>See also <a href="qscriptclass.html#queryProperty">queryProperty</a>().</p>
<hr />
<h2>Member Function Documentation</h2>
<h3 class="fn"><a name="QScriptClass"></a>QScriptClass::QScriptClass ( <a href="qscriptengine.html">QScriptEngine</a> * <i>engine</i> )</h3>
<p>Constructs a <a href="qscriptclass.html">QScriptClass</a> object to be used in the given <i>engine</i>.</p>
<p>The engine does not take ownership of the <a href="qscriptclass.html">QScriptClass</a> object.</p>
<h3 class="fn"><a name="dtor.QScriptClass"></a>QScriptClass::~QScriptClass ()&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Destroys the <a href="qscriptclass.html">QScriptClass</a> object.</p>
<p>If a <a href="qscriptclass.html">QScriptClass</a> object is deleted before the associated <a href="qscriptclass.html#engine">engine</a>(), any Qt Script objects using the <a href="qscriptclass.html">QScriptClass</a> will be &quot;demoted&quot; to normal Qt Script objects.</p>
<h3 class="fn"><a name="engine"></a><a href="qscriptengine.html">QScriptEngine</a> * QScriptClass::engine () const</h3>
<p>Returns the engine that this <a href="qscriptclass.html">QScriptClass</a> is associated with.</p>
<h3 class="fn"><a name="extension"></a><a href="http://qt.nokia.com/doc/4.6/qvariant.html">QVariant</a> QScriptClass::extension ( <a href="qscriptclass.html#Extension-enum">Extension</a> <i>extension</i>, const <a href="http://qt.nokia.com/doc/4.6/qvariant.html">QVariant</a> &amp; <i>argument</i> = QVariant() )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>This virtual function can be reimplemented in a <a href="qscriptclass.html">QScriptClass</a> subclass to provide support for extensions. The optional <i>argument</i> can be provided as input to the <i>extension</i>; the result must be returned in the form of a <a href="http://qt.nokia.com/doc/4.6/qvariant.html">QVariant</a>. You can call <a href="qscriptclass.html#supportsExtension">supportsExtension</a>() to check if an extension is supported by the <a href="qscriptclass.html">QScriptClass</a>. By default, no extensions are supported, and this function returns an invalid <a href="http://qt.nokia.com/doc/4.6/qvariant.html">QVariant</a>.</p>
<p>If you implement the Callable extension, Qt Script will call this function when an instance of your class is called as a function (e.g&#x2e; from a script or using <a href="qscriptvalue.html#call">QScriptValue::call</a>()). The <i>argument</i> will contain a pointer to the <a href="qscriptcontext.html">QScriptContext</a> that represents the function call, and you should return a <a href="http://qt.nokia.com/doc/4.6/qvariant.html">QVariant</a> that holds the result of the function call. In the following example the sum of the arguments to the script function are added up and returned:</p>
<pre> if (extension == Callable) {
     QScriptContext *context = qvariant_cast&lt;QScriptContext*&gt;(argument);
     QScriptEngine *engine = context-&gt;engine();
     double sum = 0;
     for (int i = 0; i &lt; context-&gt;argumentCount(); ++i)
         sum += context-&gt;argument(i).toNumber();
     return sum;
 }</pre>
<p>If you implement the <a href="qscriptclass.html#Extension-enum">HasInstance</a> extension, Qt Script will call this function as part of evaluating the <tt>instanceof</tt> operator, as described in ECMA-262 Section 11.8&#x2e;6&#x2e; The <i>argument</i> is a QScriptValueList containing two items: The first item is the object that <a href="qscriptclass.html#Extension-enum">HasInstance</a> is being applied to (an instance of your class), and the second item can be any value. extension() should return true if the value delegates behavior to the object, false otherwise.</p>
<p>See also <a href="qscriptclass.html#supportsExtension">supportsExtension</a>().</p>
<h3 class="fn"><a name="name"></a><a href="http://qt.nokia.com/doc/4.6/qstring.html">QString</a> QScriptClass::name () const&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns the name of the script class.</p>
<p>Qt Script uses this name to generate a default string representation of objects in case you do not provide a toString function.</p>
<p>The default implementation returns a null string.</p>
<h3 class="fn"><a name="newIterator"></a><a href="qscriptclasspropertyiterator.html">QScriptClassPropertyIterator</a> * QScriptClass::newIterator ( const <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>object</i> )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns an iterator for traversing custom properties of the given <i>object</i>.</p>
<p>The default implementation returns 0, meaning that there are no custom properties to traverse.</p>
<p>Reimplement this function if objects of your script class can have one or more custom properties (e.g&#x2e; those reported to be handled by <a href="qscriptclass.html#queryProperty">queryProperty</a>()) that you want to appear when an object's properties are enumerated (e.g&#x2e; by a for-in statement in a script).</p>
<p>Qt Script takes ownership of the new iterator object.</p>
<p>See also <a href="qscriptvalueiterator.html">QScriptValueIterator</a>.</p>
<h3 class="fn"><a name="property"></a><a href="qscriptvalue.html">QScriptValue</a> QScriptClass::property ( const <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>object</i>, const <a href="qscriptstring.html">QScriptString</a> &amp; <i>name</i>, <a href="http://qt.nokia.com/doc/4.6/qtglobal.html#uint-typedef">uint</a> <i>id</i> )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns the value of the property with the given <i>name</i> of the given <i>object</i>.</p>
<p>The <i>id</i> argument is only useful if you assigned a value to it in <a href="qscriptclass.html#queryProperty">queryProperty</a>().</p>
<p>The default implementation does nothing and returns an invalid <a href="qscriptvalue.html">QScriptValue</a>.</p>
<p>See also <a href="qscriptclass.html#setProperty">setProperty</a>() and <a href="qscriptclass.html#propertyFlags">propertyFlags</a>().</p>
<h3 class="fn"><a name="propertyFlags"></a><a href="qscriptvalue.html#PropertyFlag-enum">QScriptValue::PropertyFlags</a> QScriptClass::propertyFlags ( const <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>object</i>, const <a href="qscriptstring.html">QScriptString</a> &amp; <i>name</i>, <a href="http://qt.nokia.com/doc/4.6/qtglobal.html#uint-typedef">uint</a> <i>id</i> )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns the flags of the property with the given <i>name</i> of the given <i>object</i>.</p>
<p>The <i>id</i> argument is only useful if you assigned a value to it in <a href="qscriptclass.html#queryProperty">queryProperty</a>().</p>
<p>The default implementation returns 0.</p>
<p>See also <a href="qscriptclass.html#property">property</a>().</p>
<h3 class="fn"><a name="prototype"></a><a href="qscriptvalue.html">QScriptValue</a> QScriptClass::prototype () const&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns the object to be used as the prototype of new instances of this class (created with <a href="qscriptengine.html#newObject">QScriptEngine::newObject</a>()).</p>
<p>The default implementation returns an invalid <a href="qscriptvalue.html">QScriptValue</a>, meaning that the standard Object prototype will be used. Reimplement this function to provide your own custom prototype.</p>
<p>Typically you initialize your prototype object in the constructor of your class, then return it in this function.</p>
<p>See the &quot;Making Use of Prototype-Based Inheritance&quot; section in the <a href="http://qt.nokia.com/doc/4.6/qtscript.html">QtScript</a> documentation for more information on how prototypes are used.</p>
<h3 class="fn"><a name="queryProperty"></a><a href="qscriptclass.html#QueryFlag-enum">QueryFlags</a> QScriptClass::queryProperty ( const <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>object</i>, const <a href="qscriptstring.html">QScriptString</a> &amp; <i>name</i>, <a href="qscriptclass.html#QueryFlag-enum">QueryFlags</a> <i>flags</i>, <a href="http://qt.nokia.com/doc/4.6/qtglobal.html#uint-typedef">uint</a> * <i>id</i> )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Queries this script class for how access to the property with the given <i>name</i> of the given <i>object</i> should be handled. The given <i>flags</i> specify the aspects of interest. This function should return a subset of <i>flags</i> to indicate which aspects of property access should be further handled by the script class.</p>
<p>For example, if the <i>flags</i> contain <a href="qscriptclass.html#QueryFlag-enum">HandlesReadAccess</a>, and you would like your class to handle the reading of the property (through the <a href="qscriptclass.html#property">property</a>() function), the returned flags should include <a href="qscriptclass.html#QueryFlag-enum">HandlesReadAccess</a>. If the returned flags do not contain <a href="qscriptclass.html#QueryFlag-enum">HandlesReadAccess</a>, the property will be handled as a normal script object property.</p>
<p>You can optionally use the <i>id</i> argument to store a value that will subsequently be passed on to functions such as <a href="qscriptclass.html#property">property</a>() and <a href="qscriptclass.html#setProperty">setProperty</a>().</p>
<p>The default implementation of this function returns 0.</p>
<p>Note: This function is only called if the given property isn't already a normal property of the object. For example, say you advertise that you want to handle read access to property <tt>foo</tt>, but not write access; if <tt>foo</tt> is then assigned a value, it will become a normal script object property, and subsequently you will no longer be queried regarding read access to <tt>foo</tt>.</p>
<p>See also <a href="qscriptclass.html#property">property</a>().</p>
<h3 class="fn"><a name="setProperty"></a>void QScriptClass::setProperty ( <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>object</i>, const <a href="qscriptstring.html">QScriptString</a> &amp; <i>name</i>, <a href="http://qt.nokia.com/doc/4.6/qtglobal.html#uint-typedef">uint</a> <i>id</i>, const <a href="qscriptvalue.html">QScriptValue</a> &amp; <i>value</i> )&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Sets the property with the given <i>name</i> of the given <i>object</i> to the given <i>value</i>.</p>
<p>The <i>id</i> argument is only useful if you assigned a value to it in <a href="qscriptclass.html#queryProperty">queryProperty</a>().</p>
<p>The default implementation does nothing.</p>
<p>An invalid <i>value</i> represents a request to remove the property.</p>
<p>See also <a href="qscriptclass.html#property">property</a>().</p>
<h3 class="fn"><a name="supportsExtension"></a>bool QScriptClass::supportsExtension ( <a href="qscriptclass.html#Extension-enum">Extension</a> <i>extension</i> ) const&nbsp;&nbsp;<tt> [virtual]</tt></h3>
<p>Returns true if the <a href="qscriptclass.html">QScriptClass</a> supports the given <i>extension</i>; otherwise, false is returned. By default, no extensions are supported.</p>
<p>Reimplement this function to indicate which extensions your custom class supports.</p>
<p>See also <a href="qscriptclass.html#extension">extension</a>().</p>
<p /><address><hr /><div align="center">
<table width="100%" cellspacing="0" border="0"><tr class="address">
<td width="30%" align="left">Copyright &copy; 2010 Nokia Corporation and/or its subsidiary(-ies)</td>
<td width="40%" align="center"><a href="http://qt.nokia.com/doc/trademarks.html">Trademarks</a></td>
<td width="30%" align="right"><div align="right">Qt Solutions</div></td>
</tr></table></div></address></body>
</html>
